package model

import (
	"gin-worked/dao"
)

type SystemRole struct {
	Id         int    `json:"id"`
	SystemId   int    `gorm:"column(system_id)" json:"system_id"`
	RoleId     int    `gorm:"column(role_id)" json:"role_id"`
	Status     string `gorm:"column(status)" json:"status"`
	DeleteTime string `gorm:"column(delete_time)" json:"delete_time"`
	CreateTime string `gorm:"column(create_time)" json:"create_time"`
	UpdateTime string `gorm:"column(update_time)" json:"update_time"`
	System     System `gorm:"foreignKey:Id;references:SystemId"` //
	Role       Role   `gorm:"foreignKey:Id;references:RoleId"`   //
}

func SystemRoleAdd(data *SystemRole) (int, error) {
	err := dao.Db.Create(&data).Error
	return data.Id, err
}

func SystemRoleInfo(data *SystemRole, field []string) (SystemRole, error) {
	var systemRole SystemRole
	err := dao.Db.Select(field).Where(data).Preload("Role").First(&systemRole).Error
	return systemRole, err
}

func SystemRoleList(data *SystemRole, field []string, order string, offset int, limit int) ([]SystemRole, error) {
	var systemRole []SystemRole
	err := dao.Db.Select(field).Where(data).Preload("System").Order(order).Offset(offset).Limit(limit).Find(&systemRole).Error

	return systemRole, err
}

func SystemRoleTotal(data *SystemRole) int64 {
	var count int64
	dao.Db.Model(&SystemRole{}).Where(data).Count(&count)

	return count
}

func SystemRoleEdit(data *SystemRole, content map[string]interface{}) error {
	err := dao.Db.Model(&data).Updates(content).Error
	return err
}

func SystemRoleDel(data *SystemRole) error {
	var systemRole SystemRole
	err := dao.Db.Where(data).Delete(&systemRole).Error
	return err
}
